package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DelPicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-25 14:25
 **/
public class QuanzhouWanbao {
	private static final Logger log = LoggerFactory.getLogger(QuanzhouWanbao.class);

	public void mainMethod() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] ss = sdf.format(new Date()).split("-");
		String year = ss[0];
		String mon = ss[1];
		String day = ss[2];


		HttpResponse response = null;
		try {//https://szb.qzwb.com//qzwb/html/2021-10/25/node_1.htm
			String firstUrl = "https://szb.qzwb.com//qzwb/html/" + year + "-" + mon + "/" + day + "/node_1.htm";
			response = HttpRequest.get(firstUrl)
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.setConnectionTimeout(15000)
				.execute();
		} catch (Exception e) {
			log.error("WenzhouRuian连接失败" + e);
			return;
		}
		String resp = response.body();
		Set<String> resUrl = getUrl(resp, year, mon, day);
		getContent(resUrl);

	}

	private static void getContent(Set<String> listContent) {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("连接失败2" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//td[@class='font01']/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			StringBuffer content = new StringBuffer();
			try {
				jxDocument.sel("//founder-content/p/text()").forEach(s -> {
					content.append(s + "$$$");
				});
			} catch (Exception e) {
				continue;
			}

			List<String> newImgs = null;
			try {
				newImgs = DelPicUtil.delPic2(jxDocument.sel("//td[@align='center']/table/tbody/tr/td/img/@src"),"https://szb.qzwb.com/qzwb/");
			} catch (Exception e) {
				log.error("获取图片失败");
				continue;
			}

			Date date = new Date();

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "https://szb.qzwb.com/", "泉州-晚报");
				log.info(Thread.currentThread().getName() + "-----------------泉州-晚报----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static Set<String> getUrl(String content, String year, String mon, String day) {
		Set<String> bankuaiUrls = getBankuaiUrl(content, year, mon, day);
		Set<String> res = new HashSet<>();
		for (String bankuaiUrl : bankuaiUrls) {
			HttpResponse response;
			try {
				response = HttpRequest.get(bankuaiUrl)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
					.header("Accept-Encoding", "gzip, deflate")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				log.error("WenzhouRuian连接失败" + e);
				continue;
			}
			String resp = response.body();//https://szb.qzwb.com/qzwb/html/2021-10/25/content_556874.htm
			String pattern = "content_[0-9]{6}.htm";
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(resp);
			while (m.find()) {
				res.add("https://szb.qzwb.com/qzwb/html/" + year + "-" + mon + "/" + day + "/" + m.group());
			}
		}
		return res;
	}

	private static Set<String> getBankuaiUrl(String content, String year, String mon, String day) {
		//node_2.htm
		//https://szb.qzwb.com/qzwb/html/2021-10/25/node_2.htm
		Set<String> res = new HashSet<>();
		String pattern = "node_[0-9]{1,2}.htm";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add("https://szb.qzwb.com/qzwb/html/" + year + "-" + mon + "/" + day + "/" + m.group());
		}
		return res;
	}

//	public static void main(String[] args) {
//		String url = "https://szb.qzwb.com//qzwb/html/2021-10/25/content_556876.htm";
//		String doc = null;
//		JXDocument jxDocument = null;
//		try {
//			HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
//			doc = response.body();
//			jxDocument = new JXDocument(doc);
//		} catch (Exception e) {
//			log.info("连接失败2" + e);
//		}
//		String title = null;
//		try {
//			title = (String) jxDocument.sel("//td[@class='font01']/text()").get(0);
//		} catch (Exception e) {
//			log.error("标题错误-------------" + url + "-------------------标题错误");
//		}
//		StringBuffer content = new StringBuffer();
//		try {
//			jxDocument.sel("//founder-content/p/text()").forEach(s -> {
//				content.append(s + "$$$");
//			});
//		} catch (Exception e) {
//		}
//
//		List<String> newImgs = null;
//		try {
//			newImgs = DelPicUtil.delPic2(jxDocument.sel("//td[@align='center']/table/tbody/tr/td/img/@src"));
//		} catch (Exception e) {
//			log.error("获取图片失败");
//		}
//
//		Date date = new Date();
//	}
}
